﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace HQWeb.SysManage.HotelManage
{
    public partial class Hotel : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                BindDropDownList();
                string strType = Request["type"].Trim(); 
                if (strType.Equals("edit"))     //编辑进入
                {
                    btn_Add.CommandName = "edit";
                    hidID.Value = Request["id"];  //编辑ID
                    FillPage(hidID.Value);
                }
                else if (strType.Equals("add"))   //添加进入
                {

                }
            }
        }

        protected void FillPage(string strID)
        {
            Model.SysManage.T_PUB_HOTEL model = new Model.SysManage.T_PUB_HOTEL();
            BLL.SysManage.T_PUB_HOTEL bll = new BLL.SysManage.T_PUB_HOTEL();
            model = bll.GetModel(strID);
            txtName.Text = model.HOTEL_NAME;
            drop_desknum.SelectedValue = model.HOTEL_DESK_NUM;
            hotel_specification.SelectedValue = model.HOTEL_SPECIFICATION;
            hotel_location.Value = model.HOTEL_LOCATION;
            Remark.Text = model.DESCRIPTION;
            string strJS = "bindstyle('" + model.HOTEL_STYLE + "')";
            Universal.JS.RunJS(strJS);
        }


        #region 保存事情

        protected void btn_Add_Click(object sender, EventArgs e)
        {
            string strComandName = btn_Add.CommandName.Trim();
            BLL.SysManage.T_PUB_HOTEL bll = new BLL.SysManage.T_PUB_HOTEL();
            Model.SysManage.T_PUB_HOTEL model;
            string strID = hidID.Value.Trim();
            if (strComandName.Equals("edit"))
            {
                model = bll.GetModel(strID);
            }
            else
            {
                model = new Model.SysManage.T_PUB_HOTEL();
                model.HOTEL_ID = System.Guid.NewGuid().ToString();
            }
            model.HOTEL_NAME = txtName.Text.Trim();
            model.HOTEL_STYLE = hidHotelStyle.Value;
            model.HOTEL_DESK_NUM = drop_desknum.SelectedValue;
            model.HOTEL_LOCATION = hotel_location.Value;
            model.HOTEL_SPECIFICATION = hotel_specification.SelectedValue;
            model.DESCRIPTION = Remark.Text.Trim();
            model.BRANCH_ID = int.Parse(Session["BRANCH_ID"].ToString());
            if (strComandName.Equals("edit"))
            {
                bll.Update(model);
                Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "更新" + model.HOTEL_NAME + "酒店信息", DateTime.Now);
            }
            else
            {
                bll.Add(model);
                Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "添加" + model.HOTEL_NAME + "酒店信息", DateTime.Now);
            }

            Response.Redirect("HotelList.aspx");
        }

        #endregion

        #region 初始化页面下拉框
        public void BindDropDownList()
        {
            BLL.SysManage.T_PUB_DICTIONARY bll = new BLL.SysManage.T_PUB_DICTIONARY();
            List<Model.SysManage.T_PUB_DICTIONARY> il = bll.GetListArray(" FLAG = 1 order by ORDER_ID ");
            BindDropData(drop_desknum, 13, il);
            BindDropData(hotel_specification, 14, il);
            IEnumerable<Model.SysManage.T_PUB_DICTIONARY> lo = il.Where(p => p.PARENT_ID == 18);
            string x = "j";
            BindStyle(x, lo, addhotelstyle);
        }
        #endregion

        #region 初始化风格
        protected void BindStyle(string y, IEnumerable<Model.SysManage.T_PUB_DICTIONARY> il, System.Web.UI.HtmlControls.HtmlTableCell controls)
        {
            StringBuilder strHtml = new StringBuilder();
            strHtml.Append("  <table width=\"90%\" ");
            int count = il.Count();
            int tempValue;
            for (int j = 0; j <= (count - 1) / 5; j++)
            {
                int i = 0;
                strHtml.Append(" <tr>");
                for (; i < 5 && 5 * j + i < count; i++)
                {
                    tempValue = 5 * j + i;
                    string strName = il.ElementAt(tempValue).NAME;
                    string strId = il.ElementAt(tempValue).ID.ToString();
                    //int temp = 5 * j + i;
                    strHtml.Append("<td align=\"left\" style=\"width:20%\">");
                    strHtml.Append(" <input type=\"checkbox\" name=\"" + y + "style\" id=\"" + y + strId + "\" value=\"" + strId + "\"/>");
                    strHtml.Append(strName);
                    strHtml.Append("</td>");
                }
                if ((5 - i) > 0)
                {
                    int sy = 5 - i;
                    strHtml.Append("<td colspan=" + sy + ">");
                    strHtml.Append("</td>");
                }
            }
            strHtml.Append(" </table>");
            controls.InnerHtml = strHtml.ToString();
        }
        #endregion

        #region 通过属性类型获取属性并绑定到下拉框
        protected void BindDropData(DropDownList drop_name, int type, List<Model.SysManage.T_PUB_DICTIONARY> il)
        {

            IEnumerable<Model.SysManage.T_PUB_DICTIONARY> lo = il.Where(p => p.PARENT_ID == type);
            drop_name.ClearSelection();
            drop_name.DataSource = lo;
            drop_name.DataTextField = "NAME"; //dropdownlist的Text的字段 
            drop_name.DataValueField = "ID";//dropdownlist的Value的字段 
            drop_name.DataBind();
            ListItem item = new ListItem("", "");
            item.Selected = true;
            drop_name.Items.Add(item);
        }
        #endregion
    }
}